package org.apache.struts.pharmacy.pharmacy.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts.pharmacy.pharmacy.model.*;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v251.message.TBR_R08;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.PipeParser;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.earogya.communicate.Communicator;
import com.earogya.logic.pharmacy.request.TBR_R08_Handler;
import com.earogya.logic.pharmacy.request.CreateEQQQ04;

/***
 * Class consists with the struts action to search to a patient drug records.
 * @author klanerolle@gmail.com
 *
 */
public class SearchResultsAction extends ActionSupport{

	private List <DrugsGivenToPatient> prescriptionDrugsList;
	private Date date;
	private String messageString;

	public SearchResultsAction(){	
	}
	
	public List<DrugsGivenToPatient> getPrescriptionDrugsList() {
		return prescriptionDrugsList;
	}

	public void setPrescriptionDrugsList(
			List<DrugsGivenToPatient> prescriptionDrugsList) {
		this.prescriptionDrugsList = prescriptionDrugsList;
	}

	public String getMessageString() {
		return messageString;
	}

	public void setMessageString(String messageString) {
		this.messageString = messageString;
	}

	public SearchResultsAction(String messageString){
		this.messageString=messageString;
	}
	
	/**
	 * Overrides the execute method is ActionSupport interface,When the action occurs this method is executed.
	 * returns SUCCESS if there are no errors.
	 */
	public String execute() throws Exception {
		TBR_R08_Handler handler=new TBR_R08_Handler(createTBR_R08Msg(messageString.toString()));
		prescriptionDrugsList=handler.manageMessage();
		return SUCCESS;
	}
	
	/**
	 * Process the TBR_R08 query message and received the prescription drugs list values.
	 * @param hapiMsg to parse the object of an TBR_R08 query message
	 * @throws EncodingNotSupportedException, HL7Exception
	 */
	public TBR_R08 createTBR_R08Msg(String message) throws EncodingNotSupportedException, HL7Exception{
		Message hapiMsg  = new PipeParser().parse(message);
		return (TBR_R08)hapiMsg;
	}
	
	public void validate(){
		
	}
}
